big_enum_set 0.2.1

A library for creating sets of enums with a large number of variants.
Documentation

A library for creating enum sets that are stored as compact bit sets. The code is based on the enumset crate, except that the backing store used is an array of usize. This enables use with enums with large number of variants. The API is very similar to that of enumset.

For serde support, enable the serde feature.

Defining enums for use with BigEnumSet

Enums to be used with [BigEnumSet] should be defined using #[derive(BigEnumSetType)]:

# use big_enum_set::*;
#[derive(BigEnumSetType, Debug)]
pub enum Enum {
A, B, C, D, E, F, G,
}

For more information on more advanced use cases, see the documentation for [BigEnumSetType].

Working with BigEnumSets

BigEnumSets can be constructed via [BigEnumSet::new()] like a normal set. In addition, #[derive(BigEnumSetType)] creates operator overloads that allow you to create BigEnumSets like so:

# use big_enum_set::*;
# #[derive(BigEnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
let new_set = Enum::A | Enum::C | Enum::G;
assert_eq!(new_set.len(), 3);

All bitwise operations you would expect to work on bitsets also work on both BigEnumSets and enums with #[derive(BigEnumSetType)]:

# use big_enum_set::*;
# #[derive(BigEnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
// Intersection of sets
assert_eq!((Enum::A | Enum::B) & Enum::C, BigEnumSet::empty());
assert_eq!((Enum::A | Enum::B) & Enum::A, Enum::A);
assert_eq!(Enum::A & Enum::B, BigEnumSet::empty());

// Symmetric difference of sets
assert_eq!((Enum::A | Enum::B) ^ (Enum::B | Enum::C), Enum::A | Enum::C);
assert_eq!(Enum::A ^ Enum::C, Enum::A | Enum::C);

// Difference of sets
assert_eq!((Enum::A | Enum::B | Enum::C) - Enum::B, Enum::A | Enum::C);

// Complement of sets
assert_eq!(!(Enum::E | Enum::G), Enum::A | Enum::B | Enum::C | Enum::D | Enum::F);

The [big_enum_set!] macro allows you to create BigEnumSets in constant contexts:

# use big_enum_set::*;
# #[derive(BigEnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
const CONST_SET: BigEnumSet<Enum> = big_enum_set!(Enum::A | Enum::B);
assert_eq!(CONST_SET, Enum::A | Enum::B);

Mutable operations on the [BigEnumSet] work similarly to Rust's builtin sets:

# use big_enum_set::*;
# #[derive(BigEnumSetType, Debug)] pub enum Enum { A, B, C, D, E, F, G }
let mut set = BigEnumSet::new();
set.insert(Enum::A);
set.insert_all(Enum::E | Enum::G);
assert!(set.contains(Enum::A));
assert!(!set.contains(Enum::B));
assert_eq!(set, Enum::A | Enum::E | Enum::G);